/**
 * 缩放拖拽控制器
 * 提供 Ctrl+滚轮缩放、Ctrl+左键拖拽功能
 */
class ZoomDragController {
  constructor() {
    this.minScale = 0.5;
    this.maxScale = 3.0;
    this.scaleStep = 0.1;
    this.currentScale = 1.0;
    this.currentTranslate = { x: 0, y: 0 };
    this.isDragging = false;
    this.lastMousePos = { x: 0, y: 0 };
    this.isCtrlPressed = false;
    
    // 回调函数
    this.onScaleChange = null;
    this.onDragChange = null;
    this.onReset = null;
    
    this.init();
  }
  
  init() {
    // 监听键盘事件
    document.addEventListener('keydown', (e) => {
      if (e.ctrlKey || e.metaKey) {
        this.isCtrlPressed = true;
        
        // Ctrl+0 重置
        if (e.key === '0') {
          e.preventDefault();
          this.resetZoom();
        }
      }
    });
    
    document.addEventListener('keyup', (e) => {
      if (!e.ctrlKey && !e.metaKey) {
        this.isCtrlPressed = false;
      }
    });
    
    // 监听滚轮事件
    document.addEventListener('wheel', (e) => {
      if (this.isCtrlPressed) {
        e.preventDefault();
        
        const delta = e.deltaY > 0 ? -this.scaleStep : this.scaleStep;
        const newScale = Math.max(this.minScale, Math.min(this.maxScale, this.currentScale + delta));
        
        if (newScale !== this.currentScale) {
          this.setScale(newScale);
        }
      }
    }, { passive: false });
    
    // 监听鼠标事件
    document.addEventListener('mousedown', (e) => {
      if (this.isCtrlPressed && e.button === 0) {
        e.preventDefault();
        this.isDragging = true;
        this.lastMousePos = { x: e.clientX, y: e.clientY };
        document.body.style.cursor = 'grabbing';
      }
    });
    
    document.addEventListener('mousemove', (e) => {
      if (this.isDragging && this.isCtrlPressed) {
        e.preventDefault();
        
        const deltaX = e.clientX - this.lastMousePos.x;
        const deltaY = e.clientY - this.lastMousePos.y;
        
        this.currentTranslate.x += deltaX;
        this.currentTranslate.y += deltaY;
        
        this.lastMousePos = { x: e.clientX, y: e.clientY };
        
        this.applyTransform();
        
        if (this.onDragChange) {
          this.onDragChange(this.currentTranslate.x, this.currentTranslate.y);
        }
      }
    });
    
    document.addEventListener('mouseup', (e) => {
      if (this.isDragging) {
        this.isDragging = false;
        document.body.style.cursor = 'default';
      }
    });
    
    // 防止右键菜单
    document.addEventListener('contextmenu', (e) => {
      if (this.isCtrlPressed) {
        e.preventDefault();
      }
    });
  }
  
  setScale(scale) {
    this.currentScale = Math.max(this.minScale, Math.min(this.maxScale, scale));
    this.applyTransform();
    
    if (this.onScaleChange) {
      this.onScaleChange(this.currentScale);
    }
  }
  
  setTranslate(x, y) {
    this.currentTranslate.x = x;
    this.currentTranslate.y = y;
    this.applyTransform();
    
    if (this.onDragChange) {
      this.onDragChange(x, y);
    }
  }
  
  getScale() {
    return this.currentScale;
  }
  
  getTranslate() {
    return { ...this.currentTranslate };
  }
  
  resetZoom() {
    this.currentScale = 1.0;
    this.currentTranslate = { x: 0, y: 0 };
    this.applyTransform();
    
    if (this.onReset) {
      this.onReset();
    }
    
    if (this.onScaleChange) {
      this.onScaleChange(this.currentScale);
    }
    
    if (this.onDragChange) {
      this.onDragChange(0, 0);
    }
  }
  
  applyTransform() {
    // 查找 Flutter 应用的根容器
    const flutterView = document.querySelector('flutter-view') || 
                       document.querySelector('flt-glass-pane') ||
                       document.querySelector('#flutter-view') ||
                       document.body.querySelector('div');
    
    if (flutterView) {
      const transform = `scale(${this.currentScale}) translate(${this.currentTranslate.x}px, ${this.currentTranslate.y}px)`;
      flutterView.style.transform = transform;
      flutterView.style.transformOrigin = 'center center';
    }
  }
  
  enableZoom() {
    // 可以添加启用缩放的逻辑
  }
  
  disableZoom() {
    // 可以添加禁用缩放的逻辑
  }
  
  enableDrag() {
    // 可以添加启用拖拽的逻辑
  }
  
  disableDrag() {
    // 可以添加禁用拖拽的逻辑
  }
  
  destroy() {
    // 清理事件监听器
    document.removeEventListener('keydown', this.handleKeyDown);
    document.removeEventListener('keyup', this.handleKeyUp);
    document.removeEventListener('wheel', this.handleWheel);
    document.removeEventListener('mousedown', this.handleMouseDown);
    document.removeEventListener('mousemove', this.handleMouseMove);
    document.removeEventListener('mouseup', this.handleMouseUp);
    document.removeEventListener('contextmenu', this.handleContextMenu);
  }
}

// 将控制器暴露到全局作用域
window.ZoomDragController = ZoomDragController;